home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / bc_ti.zip / TI638.ASC < prev    next >
Text File  |  1992-02-25  |  3KB  |  133 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  C++                                    NUMBER  :  638
  9.   VERSION  :  All
  10.        OS  :  PC DOS
  11.      DATE  :  February 25, 1992                        PAGE  :  1/2
  12.  
  13.     TITLE  :  Calling Operators from Member Functions
  14.  
  15.  
  16.  
  17.  
  18.   The following provides examples  of  different  ways to invoke an
  19.   operator,  how  to  call  such  operators  from  within a  member
  20.   function and deals with some related multiple inheritance issues.
  21.  
  22.   You can always invoke an operator by name. For example, given:
  23.  
  24.                  X_Class {
  25.                  ...
  26.                    public:
  27.                      X_Class &operator +( X_Class y);
  28.                  };
  29.  
  30.                  X_Class  x, y;
  31.  
  32.   the function for the + operator can be invoked in several ways:
  33.  
  34.                  x + y;
  35.                  x.operator +(y);
  36.  
  37.   We  can think of the second example in this way: not only have we
  38.   overloaded  the  +  operator  to  add together instances  of  the
  39.   X_Class, we also created a member function of  the  X_Class whose
  40.   name is operator +.
  41.  
  42.   If X_Class has a member function foo, and we want  to  call the +
  43.   operator from  within  it,  we  would  do  it by using its member
  44.   function name:
  45.  
  46.                  X_Class::foo(X_Class &y) {
  47.                    ...
  48.                    operator +(y);
  49.                    ...
  50.                  }
  51.  
  52.   If one is dealing with multiple inheritance, and  operator  + has
  53.   been redefined in several different  classes,  one  might  get an
  54.   ambiguity at some point.  To resolve this, or any other ambiguity
  55.   the  compiler  faces  when  it  cannot  determine  which  of  the
  56.   overloaded  functions  it  must  use,  you can qualify  a  member
  57.   function name with its class name.  For example suppose we have:
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  C++                                    NUMBER  :  638
  75.   VERSION  :  All
  76.        OS  :  PC DOS
  77.      DATE  :  February 25, 1992                        PAGE  :  2/2
  78.  
  79.     TITLE  :  Calling Operators from Member Functions
  80.  
  81.  
  82.  
  83.  
  84.                            A     B
  85.                             \   /
  86.                               C
  87.  
  88.   and there is a public A & operator +( int )  defined  for  both A
  89.   and B, and both  A and B are inherited publicly into C. Then
  90.  
  91.                            A ans;
  92.                            C c1;
  93.  
  94.                            ans = c1 + 4;
  95.  
  96.   will be ambiguous.  It can be resolved by going,
  97.  
  98.                       ans = c1.B::operator +(4); or
  99.  
  100.                       ans = c1.A::operator +(4);
  101.  
  102.   depending  on which one you wanted to use.  This is because :: is
  103.   the  scope  operator.   It is used to specify which scope a given
  104.   symbol comes from.
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.